package com.byx.chatclient.model;

import android.content.Context;

import com.byx.chatclient.bean.LoginDropDownBean;
import com.byx.chatclient.contract.user.LoginTaskContract;
import com.byx.chatclient.entity.ResultCallback;
import com.byx.chatclient.entity.UserPo;
import com.byx.chatclient.factory.ManagerFactory;
import com.byx.chatclient.factory.RetrofitFactory;
import com.byx.chatclient.http.ApiUrl;
import com.byx.chatclient.manager.UserManager;
import com.byx.chatclient.pojo.LoginDto;
import com.byx.chatclient.pojo.LoginVo;
import com.byx.chatclient.utils.FileUtils;
import com.byx.chatclient.utils.ValidateUtil;

import java.util.List;
import java.util.stream.Collectors;

/**
 * LoginModel
 *
 * @author Superb
 * @date 2021/2/22 11:03
 */
public class LoginModel implements LoginTaskContract.Model {
    private static final String TAG=LoginModel.class.getSimpleName();
    private final UserManager mUserManager;

    public LoginModel(Context context) {
        this.mUserManager = ManagerFactory.getInstance().getUserManager(context);
    }

    @Override
    public void saveLoginInfo(LoginDto loginDto, String password, String localPath) {
        UserPo userPo = UserPo.builder().id(loginDto.getId()).loginId(loginDto.getLoginId())
                .nickName(loginDto.getNickName()).password(password).remoteAvatarUrl(loginDto.getAvatar())
                .lastModifyTime(loginDto.getLastModifyTime()).build();
        if (localPath != null){
            userPo.setLocalAvatarUrl(localPath);
        }
        mUserManager.saveOrUpdate(userPo);
    }

    /**
     * 获取登录信息
     *
     * @return
     */
    @Override
    public List<LoginDropDownBean> getLoginInfo() {
        List<UserPo> userPoList = mUserManager.queryLoggedInAccount();
        return userPoList.stream().map(userPo -> {
            LoginDropDownBean loginDropDownBean = new LoginDropDownBean();
            loginDropDownBean.setLoginId(userPo.getLoginId());
            loginDropDownBean.setAvatar(FileUtils.isFileExists(userPo.getLocalAvatarUrl()) ? "file://" + userPo.getLocalAvatarUrl() : userPo.getRemoteAvatarUrl());
            loginDropDownBean.setPassword(ValidateUtil.decodeByBase64(userPo.getPassword()));
            return loginDropDownBean;
        }).collect(Collectors.toList());
    }


    @Override
    public void login(LoginVo loginVo, LoginCallBack<LoginDto> callback) {
        RetrofitFactory.createApi(ApiUrl.class).login(loginVo).enqueue(new ResultCallback<LoginDto>() {
            @Override
            public void onResultSuccess(LoginDto data) {
                callback.onSuccess(data);
            }

            @Override
            public void onResultFailure(int code, String msg) {
                callback.onFailure(code,msg);
            }
        });
    }

    public UserPo queryUserPoById(String loginId){
        return mUserManager.queryUserPoByLoginId(loginId);
    }

    public void saveOrUpdateUserPo(UserPo userPo){
        mUserManager.saveOrUpdate(userPo);
    }

    public void deleteUserPo(String loginId){
        mUserManager.deleteByLoginId(loginId);
    }
}
